package com.gaobeitech.trust;

import android.os.Handler;
import android.os.Message;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

/**
 * Created by mel on 4/25/2017.
 */

public class HttpUtils {
    private static OkHttpClient client = new OkHttpClient();

    private static final String HEADER_NAME = "Authorization";
    private static String headerValue = "Basic c2J0czAxOmFkbWlu";
    private static final MediaType MEDIA_TYPE_JSON
            = MediaType.parse("application/json; charset=utf-8");

    public static void get(final String url, final Handler handler) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = createRequest(url).build();
                Response response = null;
                try {
                    response = client.newCall(request).execute();
                    System.out.println(response);
                    String result = response.body().string();
                    Message message = new Message();
                    message.obj = result;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private static Request.Builder createRequest(final String url){
         Request.Builder builder = new Request.Builder();
        if (headerValue != null){
            builder.header(HEADER_NAME, headerValue);
        }
        builder.url(url);
        return builder;
    }

    public static void post(final String url, final String postBody, final Handler handler) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Request request = createRequest(url)
                            .post(RequestBody.create(MEDIA_TYPE_JSON, postBody))
                            .build();

                    Response response = client.newCall(request).execute();

                    String result = response.body().string();
                    System.out.println(String.format("HttpClient Response [%s]", result));

                    Message message = new Message();
                    message.obj = result;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }).start();
    }

    public static void testAU(final Handler handler) {
        System.out.println("TestAU Method.");
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://10.100.103.10/core-main/api/v1/transaction/post/traininfo/sysfdzquery";
                System.out.println(String.format("Navigating - %s", url));
                Request request = createRequest(url)
                        .build();
                Response response = null;
                try {
                    response = client.newCall(request).execute();
                    System.out.println(String.format("Response - %s", response.toString()));
                    String result = response.body().string();
                    System.out.println(String.format("%s - %s", "Result",  result));
                    Message message = new Message();
                    message.obj = result;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
